<?php

namespace app\api\controller;

use app\model\Enter as EnterModel;
use app\api\validate\Enter as EnterValidate;
use think\exception\ValidateException;

// 入驻相关控制器
class Enter extends Auth
{
    /**
     * 提交入驻
     *
     * @return void
     */
    public function create()
    {
        $enter_data = [
            'shenfen'                => input('shenfen'),
            'enter_sheng'            => input('enter_sheng'),
            'enter_shi'              => input('enter_shi'),
            'enter_xian'             => input('enter_xian'),
            'enter_xiang'            => input('enter_xiang'),
            'enter_address_details'  => input('enter_address_details'),
            'boss_id'                => input('boss_id'),
            'name'                   => input('name'),
            'sex'                    => input('sex'),
            'birth_time'             => input('birth_time'),
            'id_num'                 => input('id_num'),
            'phone'                  => input('phone'),
            'census_address'         => input('census_address'),
            'census_address_details' => input('census_address_details')
        ];

        // 校验数据
        try {
            validate(EnterValidate::class)->check($enter_data);
        } catch (ValidateException $e) {
            error($e->getError());
        }

        $enter_data['user_id'] = $this->getUserId();
        $enter_data['type'] = $enter_data['shenfen'];
        $res = EnterModel::createEnter($enter_data);

        if ($res === true) {
            success('申请成功，等待审核');
        } else {
            error('申请失败');
        }
    }

    /**
     * 获取所属上级id
     *
     * @return void
     */
    public function getBossId()
    {
        $shenfen = input('shenfen');
        $enter_address = [
            'enter_sheng' => input('enter_sheng'),
            'enter_shi'   => input('enter_shi'),
            'enter_xian'  => input('enter_xian')
        ];

        if (empty($shenfen)) {
            error('申请身份不能为空');
        }
        if (!in_array($shenfen, [2, 3])) {
            error('申请身份只能是县级、乡镇合伙人');
        }

        $list = EnterModel::getBossId($shenfen, $enter_address);
        success('所属上级列表', $list);
    }
}
